package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {

    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏列表:geofenceQuery={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //之前没有对围栏的车数据进行处理，现在需要对业务数据进行补充：
        // totalNum: 围栏绑定车辆总数 availableNum: 围栏可用车辆数
        // List<VehicleVO> vehicleList: 围栏绑定车辆列表
        //1.遍历围栏集合
        for (int i=0;i<list.size();i++){
            //2.依次取出每一个围栏对象
            GeofenceVO geofenceVO = list.get(i);
            //3.调用车辆 mapper层的查询方法，查看本围栏下的所有已绑定车辆
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //4.获取当前围栏绑定车辆总数
            int totalNum = vehicleVOList.size();
            //5.定义变量保存可用车辆数
            int availableNum = 0;
            //6.依次遍历每辆车，如果车辆的状态为1（空闲），可用车辆数+1
            for(VehicleVO vehicleVO:vehicleVOList){
                if("1".equals(vehicleVO.getStatus())){
                    availableNum++;
                }
            }
            //7.将准备好的数据赋值给围栏VO的相关字段
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            //注意，此处的车辆列表详情会在后续分配车辆查看车辆详情时使用到，所以此处要一起返回
            geofenceVO.setVehicleList(vehicleVOList);

        }

        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("保存电子围栏:geofenceParam={}", geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        if (geofence.getId() != null) {
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        } else {
            geofence.setStatus("1");//启用状态
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }

    }

    @Override
    public void deleteGeofence(Long geofenceId) {
        log.debug("删除围栏:geofenceId={}", geofenceId);
        //需求:需要判断该围栏上是否还有绑定车辆,如果有绑定车辆,不能删除!
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(geofenceId);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //判断list是否为空,如果为空,可以删除围栏,如果不为空,说明围栏上还有绑定的车辆,不能删除!
        if(list.size()>0 && list!=null) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            geofenceMapper.deleteGeofence(geofenceId);
        }
    }
}












